package net.hn.hnms.biz.energy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.energy.domain.ElectricitySettlement;
import net.hn.hnms.biz.energy.mapper.ElectricitySettlementMapper;
import net.hn.hnms.biz.energy.request.ElectricitySettlementReq;
import net.hn.hnms.biz.energy.service.ElectricitySettlementService;
import net.hn.hnms.biz.energy.vo.ElectricitySettlementVo;
import net.hn.hnms.biz.energy.vo.ElectricitySettlementVo1;
import net.hn.hnms.biz.energy.vo.WaterSupplyCompanyVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.system.api.RemoteUserService;
import net.hn.hnms.sys.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ElectricitySettlementServiceImpl extends BaseService<ElectricitySettlement, ElectricitySettlementVo1> implements ElectricitySettlementService {
    @Autowired
    private ElectricitySettlementMapper electricitySettlementMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Override
    public int insert(ElectricitySettlementReq electricitySettlementReq) {
        ElectricitySettlement insertObj = new ElectricitySettlement();
        BeanUtil.copyProperties(electricitySettlementReq, insertObj);
        return electricitySettlementMapper.insert(insertObj);
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        electricitySettlementMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(ElectricitySettlementReq electricitySettlementReq) {
        ElectricitySettlement updateObj = new ElectricitySettlement();
        BeanUtil.copyProperties(electricitySettlementReq, updateObj);
        int updateCount = electricitySettlementMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public ElectricitySettlement selectDetailById(String id) {
        ElectricitySettlement electricitySettlement = electricitySettlementMapper.selectById(id);
        if (electricitySettlement == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return electricitySettlement;
    }

    @Override
    public ResponsePage<ElectricitySettlementVo1> selectPageList(ElectricitySettlementReq electricitySettlementReq, PageQuery pageQuery) {
        Page<ElectricitySettlement> page = electricitySettlementMapper.selectPage(pageQuery.build(), buildQuery(electricitySettlementReq).orderByDesc(ElectricitySettlement::getSettleDate));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        List<ElectricitySettlementVo1> list =page.getRecords().stream().map(o ->convert(o, ElectricitySettlementVo1.class)).collect(Collectors.toList());
        list.forEach(ii->{
            LoginUser userInfoById = remoteUserService.getUserInfoById(Long.valueOf(ii.getCreateUserId()));
            if (Optional.ofNullable(userInfoById).isPresent()) {
                ii.setCreateUserName(userInfoById.getNickname());
                ii.setUsername(userInfoById.getNickname());
            }

        });
//        return new ResponsePage<>(page.getRecords(), page.getTotal());
        return new ResponsePage<>(list, page.getTotal());
    }

    @Override
    public List<ElectricitySettlementVo> getEachTeamKwh(ElectricitySettlementReq req) {
//        Map<String, BigDecimal> map = new HashMap<>();
        List<String> dateList = new ArrayList<>();

        dateList.add(req.getCountYear()+"-01-01");
        dateList.add(req.getCountYear()+"-12-31");
        req.setDateTimeRange(dateList);
        List<ElectricitySettlementVo> list = electricitySettlementMapper.getEachTeamKwh(req.getMineCode(),req.getBeginDate(),req.getEndDate());
//        list.forEach(l->{
//            map.put(l.getElectricityPlaceName(),l.getSettleElectricConsumptionCount());
//        });
        return list;
    }

    @Override
    public List<ElectricitySettlementVo> getEachYearKwh(ElectricitySettlementReq req) {
        Map<String, BigDecimal> map = new HashMap<>();
        List<String> dateList = new ArrayList<>();

        dateList.add(req.getCountYear()+"-01-01");
        dateList.add(req.getCountYear()+"-12-31");
        req.setDateTimeRange(dateList);
        List<ElectricitySettlementVo> list = electricitySettlementMapper.getEachYearKwh(req.getMineCode(),req.getBeginDate(),req.getEndDate(),req.getElectricityPlaceId());
        list.forEach(l->{
            map.put(l.getCountDate(),l.getSettleElectricConsumptionCount());
        });
        return list;
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<ElectricitySettlement> buildQuery(ElectricitySettlementReq req){
        return new LambdaQueryWrapper<ElectricitySettlement>()
                .like(StrUtil.isNotBlank(req.getElectricityPlaceName()), ElectricitySettlement::getElectricityPlaceName, req.getElectricityPlaceName())
                .eq(StrUtil.isNotBlank(req.getMineCode()), ElectricitySettlement::getMineCode, req.getMineCode())
                .ge(req.getBeginDateTime() != null, ElectricitySettlement::getCreateTime, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, ElectricitySettlement::getCreateTime, req.getEndDateTime());
    }





}
